﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace University.Workshop.DataAccess
{
    public class DataAccessCistern
    {

        public static void RegisterNewCistern(Model.Cistern newCistern)
        {
            WFPEntities.Instance.DiscardChanges();
            WFPEntities.Instance.Cistern.Add(newCistern);
            WFPEntities.Instance.SaveChanges();
        }

        public static ICollection<Model.Cistern> SearchByParameter(string parameter)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = from c in WFPEntities.Instance.Cistern
                     where c.LicencePlate.Contains(parameter) || 
                           c.Person.LastNames.Contains(parameter) ||
                           c.Person.Names.Contains(parameter)
                     select c;
            return q.ToList();
        }

        public static ICollection<Model.Cistern> SearchCisternByParameterEnable(Model.Cistern cisternToFind)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = from c in WFPEntities.Instance.Cistern
                    where (c.Person.Names.StartsWith(cisternToFind.Person.Names) &&
                          c.Person.LastNames.StartsWith(cisternToFind.Person.LastNames) &&
                          c.LicencePlate.StartsWith(cisternToFind.LicencePlate)) && (c.IsActive)
                    select c;
            return q.ToList();
        }

        public static ICollection<Model.Cistern> SearchCisternByParameterDisable(Model.Cistern cisternToFind)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = from c in WFPEntities.Instance.Cistern
                    where (c.LicencePlate.StartsWith(cisternToFind.LicencePlate) && (c.IsActive==false))
                    select c;
            return q.ToList();
        }

        public static ICollection<Model.Cistern> SearchCisternByParameterAll(Model.Cistern cisternToFind)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = from c in WFPEntities.Instance.Cistern
                    where (
                            ((c.Person.Names.StartsWith(cisternToFind.Person.Names) &&
                            c.Person.LastNames.StartsWith(cisternToFind.Person.LastNames))||
                            (c.DriverId==null))
                          ) &&
                          c.LicencePlate.StartsWith(cisternToFind.LicencePlate)
                    select c;
            return q.ToList();
        }

        public static ICollection<Model.Cistern> SearchCisternByPlate(Model.Cistern mp)
        {
            WFPEntities.Instance.DiscardChanges();
            return      (from c in WFPEntities.Instance.Cistern
                        where c.LicencePlate == mp.LicencePlate
                        select c).ToList();
        }

        public static void UpdateCistern(Model.Cistern cistern)
        {
            WFPEntities.Instance.DiscardChanges();
            var c = (from p in WFPEntities.Instance.Cistern
                     where p.LicencePlate == cistern.LicencePlate
                     select p).First();
            c.Capacity = cistern.Capacity;
            c.DriverId = cistern.DriverId;
            c.IsActive = true;
            WFPEntities.Instance.SaveChanges();
        }

        public static void DeletePersonFromCistern(Model.Cistern CisternForDisable)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = (from v in WFPEntities.Instance.Cistern
                     where v.LicencePlate == CisternForDisable.LicencePlate
                     select v).First();
            q.Person = null;
            q.DriverId = null;
            q.IsActive = false;
            WFPEntities.Instance.SaveChanges();
        }

    }
}
